package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//菜品模块
@RestController
public class DishController {
    @Autowired
    private DishService dishService;

    // 分页+条件查询
    @GetMapping("/dish/page")
    public ResultInfo findByPage(
            @RequestParam(name = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            String name) { // 1.接收参数
        // 2.调用service
        Page<Dish> page = dishService.findByPage(pageNum, pageSize, name);
        // 3.返回结果
        return ResultInfo.success(page);
    }


    // 新增菜品
    @PostMapping("/dish")
    public ResultInfo save(@RequestBody Dish dish) { // 1.接收post请求参数
        // 2.调用service新增
        dishService.save(dish);
        // 3.返回成功
        return ResultInfo.success();
    }

    // 回显菜品
    @GetMapping("/dish/{id}")
    public ResultInfo findById(@PathVariable Long id) {// 1.接收路径参数
        // 2.调用service查询
        Dish dish = dishService.findById(id);
        // 3.返回结果
        return ResultInfo.success(dish);
    }


    // 修改菜品
    @PutMapping("/dish")
    public ResultInfo updateById(@RequestBody Dish dish) { // 1.接收put请求参数
        // 2.调用service修改
        dishService.updateById(dish);
        // 3.返回结果
        return ResultInfo.success();
    }

    //删除菜品
    @DeleteMapping("/dish")
    public ResultInfo deleteBatchIds(@RequestParam List<Long> ids){ //如果使用集合接收参数: 需使用@RequestParam,否则报错
        // 2.调用service删除
        dishService.deleteBatchIds(ids);
        // 3.返回成功
        return ResultInfo.success();
    }

    //菜品起售、停售
    @PostMapping("/dish/status/{status}")
    public ResultInfo updateStatus(@PathVariable Integer status,@RequestParam List<Long> ids){
        // 2.调用service
        dishService.updateStatus(status,ids);
        // 3.返回成功
        return ResultInfo.success();
    }

    // 根据分类查询菜品列表
    @GetMapping("/dish/list")
    public ResultInfo findListByCategoryId(Long categoryId, String name) {// 1.接收参数
        // 2.调用service
        List<Dish> dishList = dishService.findListByCategoryId(categoryId , name);
        // 3.返回结果
        return ResultInfo.success(dishList);
    }
}
